package com.baixun.controller;


import com.baixun.constant.MessageConstant;
import com.baixun.entity.PageResult;
import com.baixun.entity.QueryPageBean;
import com.baixun.entity.Result;
import com.baixun.pojo.User;
import com.baixun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    HttpSession httpSession;

    /**
     * 新增用户首先判断登录名以及电话号码是否存在
     */
    @GetMapping("/findByConditon")
    public Result findByConditon(@RequestBody User user){
        try {
             user = userService.findByCondition(user);
             if(user != null){
                 return new Result(false,"已经存在，请换一个！",user);
            }else{
                 return new Result(true,MessageConstant.QUERY_USER_SUCCESS);
             }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_USER_FAIL,e);

        }
    }


    /**
     * 新增用户,并且同时可以选择划分角色
     * @return
     */
    @PostMapping("/add")
    public Result addUser(@RequestBody User user){

        try {
            userService.addUser(user);
            return new Result(true, MessageConstant.ADD_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL,e.getMessage());
        }
    }

    //通过id查询用户
    @GetMapping("/findUser/{id}")
    public Result findUserById(@PathVariable("id") Integer id){

        try {
           User user = userService.findUserById(id);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL,e);
        }
    }

    //通过条件分页查询用户
    @GetMapping("/findPageUser")
    public Result findUserByCondition(@RequestBody QueryPageBean<User> queryPageBean){

        try {
            PageResult pageResult = userService.findUserByConditionAndPage(queryPageBean);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }

    //修改用户
    @PutMapping("/update")
    public Result updateUserById(@RequestBody User user){

        try {
            userService.updateUserById(user);
            return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_USER_FAIL,e.getMessage());
        }
    }

    //永久删除用户
    @DeleteMapping("delete/{id}")
    public Result deleteById(@PathVariable("id") Integer id){

        try {
            userService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_USER_FAIL,e.getMessage());
        }
    }

    //更改用户的状态
    @PatchMapping("tempDelete/{id}/{status}")
    public Result tempDeleteUser(@PathVariable("id") Integer id,@PathVariable("status") Byte status){

        try {
            userService.tempDeleteUser(id,status);
            return new Result(true,MessageConstant.UPDATE_USERSTATUS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_USERSTATUS_FAIL,e);
        }
    }

    /**
     * 登录
     * @param loginName
     * @param password
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestParam("loginName") String loginName,@RequestParam("password") String password){

        try {
            User user = userService.login(loginName,password);
            //登录成功
            if(user != null){
                httpSession.setAttribute("loginName",loginName);
                httpSession.setAttribute("login_time",user.getLoginTime());
                return new Result(true,MessageConstant.LOING_SUCCESS);
            }else{
                return new Result(false,MessageConstant.USER_PASSWORD_ERROR);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.LOGIN_FAIL,e.getMessage());
        }
    }


}
